package com.dinzeer.slashblade_useful_addon.events.bladestand;

import com.dinzeer.slashblade_useful_addon.config.SoulCrystalConfig;
import com.dinzeer.slashblade_useful_addon.register.SUAItem;
import mods.flammpfeil.slashblade.capability.slashblade.ISlashBladeState;
import mods.flammpfeil.slashblade.event.SlashBladeEvent;
import net.minecraft.network.protocol.PacketUtils;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.Tags;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class SoulCrystalUse {
    @SubscribeEvent
    public static void soulCrystalHit(SlashBladeEvent.BladeStandAttackEvent event){
        if (!(event.getDamageSource().getEntity() instanceof Player player)) return;
        ISlashBladeState state=event.getSlashBladeState();
        if (player.getMainHandItem().getItem()== SUAItem.soul_crystal.get()){
            state.setKillCount(state.getKillCount()+ SoulCrystalConfig.SoulCrystalKillCount.get());
            state.setProudSoulCount(state.getProudSoulCount()+ SoulCrystalConfig.SoulCrystalProudSoul.get());
            state.setRefine(state.getRefine()+ SoulCrystalConfig.SoulCrystalRefine.get());
            state.setColorCode(state.getColorCode()+1);
            player.getMainHandItem().shrink(1);
            event.setCanceled(true);
        }
        if (player.getMainHandItem().getItem()== SUAItem.god_soul_crystal.get()){
            state.setKillCount(state.getKillCount()+  SoulCrystalConfig.SoulCrystalKillCount.get()*10);
            state.setProudSoulCount(state.getProudSoulCount()+SoulCrystalConfig.SoulCrystalProudSoul.get()*10);
            state.setRefine(state.getRefine()+  SoulCrystalConfig.SoulCrystalRefine.get()*10);
            state.setColorCode(state.getColorCode()+111);
            player.getMainHandItem().shrink(1);
            event.setCanceled(true);
        }
    }
    @SubscribeEvent
    public static void SoulCrystalDrop(LivingDeathEvent event){
        if (SoulCrystalConfig.SoulCrystalDrop.get()){
        if (event.getEntity().getType().is(Tags.EntityTypes.BOSSES)){

            int dropCount= 1+(int) ((event.getEntity().getMaxHealth()-SoulCrystalConfig.SoulCrystalDropThreshold.get())/SoulCrystalConfig.SoulCrystalDropDivisor.get());
            if (dropCount>SoulCrystalConfig.SoulCrystalDropMax.get()){
                dropCount=SoulCrystalConfig.SoulCrystalDropMax.get();
            }
            if(dropCount<0){
                dropCount=1;
            }

            for (int i=0;i<dropCount;i++){
          event.getEntity().spawnAtLocation(new ItemStack(SUAItem.soul_crystal.get()));
            }
        }
        }
    }
}
